<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="账号" prop="account">
      <el-input v-model="dataForm.account" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="组织ID
#c_core_org" prop="orgId">
      <el-input v-model="dataForm.orgId" placeholder="组织ID
#c_core_org"></el-input>
    </el-form-item>
    <el-form-item label="岗位ID
#c_core_station" prop="stationId">
      <el-input v-model="dataForm.stationId" placeholder="岗位ID
#c_core_station"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机"></el-input>
    </el-form-item>
    <el-form-item label="性别
#Sex{W:女;M:男;N:未知}" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别
#Sex{W:女;M:男;N:未知}"></el-input>
    </el-form-item>
    <el-form-item label="启用状态 1启用 0禁用" prop="status">
      <el-input v-model="dataForm.status" placeholder="启用状态 1启用 0禁用"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="avatar">
      <el-input v-model="dataForm.avatar" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="工作描述
比如：  市长、管理员、局长等等   用于登陆展示" prop="workDescribe">
      <el-input v-model="dataForm.workDescribe" placeholder="工作描述
比如：  市长、管理员、局长等等   用于登陆展示"></el-input>
    </el-form-item>
    <el-form-item label="最后一次输错密码时间" prop="passwordErrorLastTime">
      <el-input v-model="dataForm.passwordErrorLastTime" placeholder="最后一次输错密码时间"></el-input>
    </el-form-item>
    <el-form-item label="密码错误次数" prop="passwordErrorNum">
      <el-input v-model="dataForm.passwordErrorNum" placeholder="密码错误次数"></el-input>
    </el-form-item>
    <el-form-item label="密码过期时间" prop="passwordExpireTime">
      <el-input v-model="dataForm.passwordExpireTime" placeholder="密码过期时间"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="最后登录时间" prop="lastLoginTime">
      <el-input v-model="dataForm.lastLoginTime" placeholder="最后登录时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          account: '',
          name: '',
          orgId: '',
          stationId: '',
          email: '',
          mobile: '',
          sex: '',
          status: '',
          avatar: '',
          workDescribe: '',
          passwordErrorLastTime: '',
          passwordErrorNum: '',
          passwordExpireTime: '',
          password: '',
          lastLoginTime: ''
        },
        dataRule: {
          account: [
            { required: true, message: '账号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          orgId: [
            { required: true, message: '组织ID
#c_core_org不能为空', trigger: 'blur' }
          ],
          stationId: [
            { required: true, message: '岗位ID
#c_core_station不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别
#Sex{W:女;M:男;N:未知}不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '启用状态 1启用 0禁用不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          workDescribe: [
            { required: true, message: '工作描述
比如：  市长、管理员、局长等等   用于登陆展示不能为空', trigger: 'blur' }
          ],
          passwordErrorLastTime: [
            { required: true, message: '最后一次输错密码时间不能为空', trigger: 'blur' }
          ],
          passwordErrorNum: [
            { required: true, message: '密码错误次数不能为空', trigger: 'blur' }
          ],
          passwordExpireTime: [
            { required: true, message: '密码过期时间不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          lastLoginTime: [
            { required: true, message: '最后登录时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/auth/authuser/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.account = data.authUser.account
                this.dataForm.name = data.authUser.name
                this.dataForm.orgId = data.authUser.orgId
                this.dataForm.stationId = data.authUser.stationId
                this.dataForm.email = data.authUser.email
                this.dataForm.mobile = data.authUser.mobile
                this.dataForm.sex = data.authUser.sex
                this.dataForm.status = data.authUser.status
                this.dataForm.avatar = data.authUser.avatar
                this.dataForm.workDescribe = data.authUser.workDescribe
                this.dataForm.passwordErrorLastTime = data.authUser.passwordErrorLastTime
                this.dataForm.passwordErrorNum = data.authUser.passwordErrorNum
                this.dataForm.passwordExpireTime = data.authUser.passwordExpireTime
                this.dataForm.password = data.authUser.password
                this.dataForm.lastLoginTime = data.authUser.lastLoginTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/auth/authuser/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'account': this.dataForm.account,
                'name': this.dataForm.name,
                'orgId': this.dataForm.orgId,
                'stationId': this.dataForm.stationId,
                'email': this.dataForm.email,
                'mobile': this.dataForm.mobile,
                'sex': this.dataForm.sex,
                'status': this.dataForm.status,
                'avatar': this.dataForm.avatar,
                'workDescribe': this.dataForm.workDescribe,
                'passwordErrorLastTime': this.dataForm.passwordErrorLastTime,
                'passwordErrorNum': this.dataForm.passwordErrorNum,
                'passwordExpireTime': this.dataForm.passwordExpireTime,
                'password': this.dataForm.password,
                'lastLoginTime': this.dataForm.lastLoginTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
